<?php
namespace app\admin\controller;

class Post extends Base
{
	public $name='post';
    public function index()
    {
        $map[]=['cid','=',input('cid')];
		if(input('name')){
			$map[]=['name','like',"%".input('name')."%"];
		}
        $list = db($this->name)->where($map)->order("id desc")->paginate(20);
		 
        $this->assign('list', $list);
		$cate=db('cate')->column('*','id');
		$this->assign('cate', $cate);
        \cookie('url', \request()->url());
        return $this->fetch();
    }
	
	 
    public function add()
    {
		$cate=db_find('cate',"id=".input('cid'));
        if ($this->request->isAjax()) { 
            $data=input('param.');
            $data['time_add']=time();
			$post=model('post');
			$post->allowField(true)->save($data);
			if(empty($data['link'])){
				
				$post->url=$cate['url'].$post->id;
				$post->save();
			}
            $this->success("添加成功", cookie('url'));
        }
		$this->assign('cate',$cate);
        return $this->fetch();
    }
       
    public function edit()
    {
        $map['id']=input('id');
        $vo = db($this->name)->where($map)->find();
		$cate=db_find('cate',"id=".$vo['cid']);
        if ($this->request->isAjax()) {
            $data=input('post.');
			$post=model('post');
			if(empty($data['link'])){
				$data['url']=$cate['url'].$vo['id'];
			}
			$post->allowField(true)->save($data,['id'=>input('id')]);
            $this->success("操作成功", cookie('url'));
        }
		$this->assign('cate',$cate);
        $this->assign("vo", $vo);
        return $this->fetch();
    }
    public function dele()
    {
		$map['id']=input('id');
        db($this->name)->where($map)->delete();
        $this->success("操作成功", cookie('url'));
    }
}
